//
// ConfigPage
//
// Copyright (C) jextra.net.
//
//  This file is part of the Crack build system.
//
//  The Crack build system is free software; you can redistribute it 
//  and/or modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Crack build system is distributed in the hope that it will be 
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Crack build system; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.crackupdaterweb;

import com.javapepper.core.*;
import com.javapepper.page.*;
import com.javapepper.page.binding.*;
import com.javapepper.widget.*;
import java.io.*;
import java.util.*;
import javax.servlet.http.*;

public class ConfigPage extends PepPage
{
    // ============================================================
    // Fields
    // ============================================================

    @PepSessionVariable( "configModel" )
    private ConfigModel _model;

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    @Override
    public String getTitle()
    {
        return "Crack Updater Configuration";
    }

    @Override
    public Collection<PepWidget> buildWidgets( PepSession pepSession, HttpServletRequest request,
        HttpServletResponse response )
        throws Exception
    {
        addStyleSheet( "/crackupdater/css/crackupdater.css" );

        ArrayList<PepWidget> widgets = new ArrayList<PepWidget>();

        widgets.add( new PepImage( "/crackupdater/img/crack.png" ) );
        widgets.add( new PepHeader( 1, "Configuration" ) );

        PepPanel links = new PepPanel( "links" );
        links.setStyleClass( "links" );
        widgets.add( links );
        links.add( new PepLink( "/crackupdater", "Goto CrackUpdater Home Page" ) );

        PepForm form = new PepForm( "form" );
        widgets.add( form );
        PepInputPanel panel = new PepInputPanel();
        form.add( panel );

        PepStringInput updaterRootDir = new PepStringInput( "updaterRootDir" );
        updaterRootDir.setValueBinding( new PepMutableValueBinding<String>()
        {
            public String getValue()
                throws Exception
            {
                File dir = _model.getUpdaterRootDir();
                return dir == null ? null : dir.getAbsolutePath();
            }

            public void setValue( String value )
                throws Exception
            {
                _model.setUpdaterRootDir( value.trim().isEmpty() ? null : new File( value ) );
            }
        }
        );
        panel.add( "CrackUpdater root directory (leave blank if none)", updaterRootDir );
        if ( !_model.isUpdaterRootDirValid() )
        {
            updaterRootDir.setStyleClass( "error" );
            PepText errorText = new PepText( "Directory does not exist" );
            errorText.setStyleClass( "errorText" );
            panel.add( "", errorText );
        }

        //
        // Buttons
        //
        PepPanel buttons = new PepPanel( "buttons" );
        buttons.setStyleClass( "buttons" );

        PepSubmitButton saveButton = new PepSubmitButton( "Save" );
        saveButton.setMethodBinding( new PepMethodBinding()
        {
            public PepMethodBindingAction callMethod()
                throws Exception
            {
                save();
                return null;
            }
        }
        );
        buttons.add( saveButton );

        PepSubmitButton revertButton = new PepSubmitButton( "Revert" );
        revertButton.setMethodBinding( new PepMethodBinding()
        {
            public PepMethodBindingAction callMethod()
                throws Exception
            {
                revert();
                return null;
            }
        }
        );
        buttons.add( revertButton );

        form.add( buttons );

        return widgets;
    }

    // ----------
    // private
    // ----------

    private void save()
        throws IOException
    {
        if ( !_model.isUpdaterRootDirValid() )
        {
            return;
        }

        _model.save();
        _model.read();
    }

    private void revert()
        throws IOException
    {
        _model.read();
    }
}

